package com.coder520.sync_learning.demo;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedReader;
import java.io.PipedWriter;

public class Piped {
	public static void main(String[] args) throws IOException {
		PipedWriter out=new PipedWriter();
		PipedReader in=new PipedReader();
		
//		将输入输出流进行连接，否则在使用时会抛出IOException
		
		out.connect(in);
		
		Thread printThread=new Thread(new Print(in),"PrintThread");
		printThread.start();
		
		int receive=0;
		
		try {
			while ((receive=in.read())!=-1) {
				out.write(receive);
			}
		}finally {
			out.close();
		}
	}
	
	static class Print implements Runnable{
		private PipedReader in;
		public Print(PipedReader in) {
			// TODO Auto-generated constructor stub
			this.in=in;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			int receive=0;
			try {
				while((receive=in.read())!=-1) {
					System.out.println((char)receive);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
	}
}
